﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Adex;
using MediaMonitor.Common;

namespace MediaMonitor.Admin
{
    public partial class Data_Mgmt_Media : Adex.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lstMediaType.DataSourceID = "sqlDSMediaType";
                lstMediaType.DataTextField = "MediaTypeName";
                lstMediaType.DataValueField = "MediaTypeCode";
                lstMediaType.DataBind();

                Utility.SelectListItemByText(lstMediaType, "TV");
            }
        }

        protected void lstMediaType_SelectedIndexChanged(object sender, EventArgs e)
        {
            radGrid.Rebind();
        }

        protected void radGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            DataTable dt = Adex.Medium.Query().SetSelectList("*, CONVERT(VARCHAR, DateModified, 101) AS LastModifiedDate").WHERE("MediaTypeCode", lstMediaType.SelectedValue.ToString()).ExecuteDataSet().Tables[0];
            radGrid.DataSource = dt;

            //radGrid.GroupingSettings.CaseSensitive = f1alse;
        }

        protected void radGrid_DeleteCommand(Object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            //check deletion of child rows
            if (e.Item.OwnerTableView.DataSourceID == "sqlDSProg")
            {
                string detailPkId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProgramCode"].ToString();
                try
                {
                    Program.Delete(detailPkId);
                }
                catch (Exception ex)
                {
                    AjaxFuncs.MsgBox(this, "Failed to delete this record, because related data (child records) exists in other tables." + ex.Message);
                }

                return;
            }


            string pkId = radGrid.MasterTableView.DataKeyValues[e.Item.ItemIndex]["MediaCode"].ToString();

            try
            {
                Medium.Delete(pkId);

            }
            catch
            {
                AjaxFuncs.MsgBox(this, "Failed to delete this record, because related data (child records) exists in other tables.");
            }


            radGrid.Rebind();
        }

        protected void radGrid_UpdateCommand(Object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            //string detailPkId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["MediaCode"].ToString();
            //string MediaName = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["MediaName"].ToString();
            //MediaName = MediaName.Replace(" Edit", "");
            //sqlDSProg.UpdateParameters.Add("MediaCode", detailPkId);
            //sqlDSProg.UpdateParameters.Add("MediaName", MediaName);
            //sqlDSProg.Update();
        }
        protected void btnAddNew_Clicked(object sender, EventArgs e)
        {

            if (Medium.Query().WHERE("MediaCode", tbMediaCode.Text.Trim()).GetCount("MediaCode") > 0)
            {
                AjaxFuncs.MsgBox(this.Page, "This media code already exists");
                return;
            }

            if (Medium.Query().WHERE("MediaName", tbMediaName.Text.Trim()).AND("MediaTypeCode", lstMediaType.SelectedValue).GetCount("MediaCode") > 0)
            {
                AjaxFuncs.MsgBox(this.Page, "Cannot Add this Media record. Another Channel / media already exists under " + lstMediaType.SelectedItem.Text);
                return;
            }

            Medium md = new Medium();
            md.MediaCode = Utility.ToInt(tbMediaCode.Text.Trim());
            md.MediaName = tbMediaName.Text.Trim();
            md.MediaTypeCode = Utility.ToInt(lstMediaType.SelectedValue);

            md.Save();

            AjaxFuncs.MsgBox(this.Page, "New Media / channel saved successfully.");


            radGrid.Rebind();




        }
    }
}
